package com.group6.exam.manager.dao;

import java.util.List;
import java.util.Map;

import com.group6.exam.common.entity.PageModel;
import com.group6.exam.common.util.DBUtil;

public class GradeDaoImpl implements IGradeDao {

	// 根据班级编号删除班级的方法
	@Override
	public int deleteGradeById(String gradeId) {
		String sql = "DELETE FROM grade WHERE grade_id=?";
		return DBUtil.executeUpdate(sql, gradeId);
	}

	// 修改前查询
	@Override
	public Map<String, Object> preUpdateGradeById(String gradeId) {
		String sql = "SELECT * FROM grade WHERE grade_id=?";
		List<Map<String, Object>> list = DBUtil.executeQuery(sql, gradeId);
		return list != null && list.size() > 0 ? list.get(0) : null;
	}

	// 修改
	@Override
	public int update(String gradeName, String gradeId) {
		String sql = "UPDATE grade SET grade_name=? WHERE grade_id=?";
		return DBUtil.executeUpdate(sql, gradeName, gradeId);
	}

	// 更新
	@Override
	public int update(String gradeName) {
		String sql = "INSERT INTO grade VALUES(NULL,?)";
		return DBUtil.executeUpdate(sql, gradeName);
	}

	// 分页查询方法
	@Override
	public List<Map<String, Object>> getPagingByPageModel(PageModel pageModel) {
		// 获取每页第一条记录的索引(当前页-1)*每页显示数
		String sql = "SELECT * FROM grade LIMIT ?,?";
		int index = (pageModel.getCurrentPage() - 1) * pageModel.getPageSize();
		int pageSize = pageModel.getPageSize();
		return DBUtil.executeQuery(sql, index, pageSize);
	}

	// 获取总记录数
	@Override
	public int getTotalRecords() {
		String sql = "SELECT * FROM grade";
		return DBUtil.executeQuery(sql).size();
	}

}
